package com.kltn.zigo.zingme.sdk;

import android.app.Activity;
import android.util.Log;

import com.android.zing.ZME_Base;
import com.android.zing.ZME_ZC_BillingListener;
import com.android.zing.ZME_ZC_DepositListener;
import com.android.zing.ZME_ZC_HistoryListener;
import com.android.zingcredits.ZC_Environment;
import com.android.zingcredits.ZC_ZCredits;

public class ZingCreditsSDK {
	
	private static String TAG = "ZingCreditsSDK";
		
	private ZC_ZCredits mAsyncZingCredits = null;
	
	private ZME_Base mZme;
	
//	private final ZC_Environment mEnv = ZC_Environment.DEVELOPMENT;
	private final ZC_Environment mEnv = ZC_Environment.PRODUCTION; //enable when deposit on production enviroment
	
	private static ZingCreditsSDK mInstance = null;
	
	public static ZingCreditsSDK getInstance(ZME_Base zme) {
		if(mInstance == null) {
			mInstance = new ZingCreditsSDK(zme);
		}
		return mInstance;
	}
	
	
	public ZingCreditsSDK(ZME_Base zme) {
		this.mZme = zme;		
	}
	
	public ZC_ZCredits getZC_ZCredits() {
		if(mAsyncZingCredits == null) mAsyncZingCredits = new ZC_ZCredits(mZme, mEnv);
		return mAsyncZingCredits;
	}
	
	///////////////////// for zc ///////////////
	/***
	 * This function used to request deposit to ZingCredits
	 * @param ctx : Android Application Context
	 * @param appIdOfZingCredits :  appid of ZingCredits system
	 * @param listener : listener is a callback to parse data returned
	 */
	public void requestDeposit(final Activity activityContext, String appIdOfZingCredits, final ZME_ZC_DepositListener listener) {
		try {
			getZC_ZCredits().requestDeposit(activityContext, appIdOfZingCredits, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in requestDeposit", ex);
		}
	}
	
	/***
	 * This function used to request bill something
	 * 
	 * @param ctx : Android Applicatoin Context
	 * 
	 * @param appIdOfZingCredits : appid of ZingCredits system
	 * 
	 * @param data : blling data encoded by zcypher (ref to ZingCredits document) 
	 * 
	 * @param state : a value provided by 3rd-party and it will be return back
	 * if invite success to prevent CSRF attacks
	 * 
	 * @param listener
	 */
	public void requestBill(final Activity activityContext, String appIdOfZingCredits, String data, String state, final ZME_ZC_BillingListener listener) {
		try {
			getZC_ZCredits().requestBill(activityContext, appIdOfZingCredits, data, state, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in requestBill", ex);
		}
	}
	
	/***
	 * This function used to show transaction history of user
	 * @param ctx : Android Application Context 
	 * @param appIdOfZingCredits : appid of ZingCredits system
	 * @param listener : listener is a callback to parse data returned
	 */
	public void showTranxHistory(final Activity activityContext, String appIdOfZingCredits, final ZME_ZC_HistoryListener listener) {
		try {
			getZC_ZCredits().showHistory(activityContext, appIdOfZingCredits, listener);
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in requestBill", ex);
		}
	}
	
	/***
	 * For testing purpose only. This function must generate data encoded use for test requestBill function. 
	 * App/Game must encode data on server side according document of ZingCredits to avoid leak of key1 and key2 of ZingCredits
	 * @return data encoded
	 */
	public String genDataForTestOnly() {
		String data = "";
		try {
			data = getZC_ZCredits().genDataForTestOnly();
		}
		catch(Exception ex) {
			Log.e(TAG, "Exception in genDataForTestOnly", ex);
		}
		return data;
	}
	
	public String getAppIdOfZingCreditsForTestOnly() {
		return "testgame";
	}
}
